package org.example.im.server.handler.impl.biz;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.example.im.bean.protocol.CommandType;
import org.example.im.bean.protocol.request.LogoutRequestPacket;
import org.example.im.bean.protocol.response.LogoutResponsePacket;
import org.example.im.common.util.SessionUtil;
import org.example.im.server.handler.ServerHandler;
import org.springframework.stereotype.Component;

/**
 * @author dingrui
 * @since 2022/2/12
 * @description 登出
 */
@Component
public class LogoutHandler extends SimpleChannelInboundHandler<LogoutRequestPacket> implements ServerHandler {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, LogoutRequestPacket request) throws Exception {
        SessionUtil.unBindSession(ctx.channel());
        LogoutResponsePacket response = LogoutResponsePacket.builder().success(true).build();
        ctx.writeAndFlush(response);
    }

    @Override
    public CommandType genType() {
        return CommandType.LOGOUT_REQUEST;
    }
}
